package com.chen.dao.impl;



import com.chen.dao.CategoryDao;
import com.chen.entity.Category;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CategoryDaoImp implements CategoryDao {
    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(Category obj) {
        String sql="INSERT INTO  tb_category(category_id,category_name) VALUES (?,?)";
        Object[] paras={obj.getCategory_id(),obj.getCategory_name()};
        try {
            int row = qr.update(conn, sql, paras);
            conn.close();
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Category obj) {
        String sql="UPDATE  tb_category SET  category_name=? WHERE category_id=? ";
        Object[] paras={obj.getCategory_name(),obj.getCategory_id()};
        try {
            int row = qr.update(conn, sql, paras);
            conn.close();
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql="DELETE FROM tb_category WHERE category_id  = ? ";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            conn.close();
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Category findById(Object key) {
        String sql="SELECT * FROM tb_category where category_id  = ?";
        Object[] paras={key};
        try {
            Category category=qr.query(conn,sql,paras,new BeanHandler<Category>(Category.class));
            conn.close();
            return category;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Category> findAll() {
        String sql="SELECT * FROM tb_category";
        Object[] paras={};
        try {
            List<Category> categoryList=qr.query(conn,sql,paras,new BeanListHandler<Category>(Category.class));
            conn.close();
            return categoryList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Category> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Category> findPage(int page, int pagesize) {
        String sql="SELECT * FROM tb_category limit ?,?";
        Object[] paras={(page-1)*pagesize,pagesize};
        try {
            List<Category> categoryList=qr.query(conn,sql,paras,new BeanListHandler<Category>(Category.class));
            conn.close();
            return categoryList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql="select count(*) from tb_category";
        Object[] paras={};
        try {
            long nums=qr.query(conn,sql,paras,new ScalarHandler<Long>());
            conn.close();
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
